package model;

import java.sql.*;
import java.util.Date;
import dal.*;
import util.*;

public class ChatModel implements IModel {

	public static class FieldNames {
		public static String Id = "Id";
		public static String Sender = "Sender";
		public static String Receiver = "Receiver";
		public static String Message = "Message";
		public static String SentDate = "SentDate";

	}

	public class ErrorMessages {
		public String Id = "";
		public String Sender = "";
		public String Receiver = "";
		public String Message = "";
		public String SentDate = "";

	}

	public ErrorMessages errMsgs = new ErrorMessages();

	public String[] errMsgList = null;

	// private variables
	private int _id = Integer.MIN_VALUE;
	private String _sender = null;
	private String _receiver = null;
	private String _message = null;
	private Date _sent_date = null;

	public ChatModel() {
	}

	// get/set
	public int getId() {
		return _id;
	}

	public void setId(int value) {
		_id = value;
	}

	public String getIdToString() {
		if (_id == Integer.MIN_VALUE || _id == Integer.MAX_VALUE) {
			return "";
		} else {
			return Integer.toString(_id);
		}
	}

	public String getSender() {
		return _sender;
	}

	public void setSender(String value) {
		_sender = value;
	}

	public String getSenderToString() {
		if (_sender != null) {
			return HtmlUtil.genHtml(_sender);
		} else {
			return "";
		}
	}

	public String getReceiver() {
		return _receiver;
	}

	public void setReceiver(String value) {
		_receiver = value;
	}

	public String getReceiverToString() {
		if (_receiver != null) {
			return HtmlUtil.genHtml(_receiver);
		} else {
			return "";
		}
	}

	public String getMessage() {
		return _message;
	}

	public void setMessage(String value) {
		_message = value;
	}

	public String getMessageToString() {
		if (_message != null) {
			return HtmlUtil.genHtml(_message);
		} else {
			return "";
		}
	}

	public Date getSentDate() {
		return _sent_date;
	}

	public void setSentDate(Date value) {
		_sent_date = value;
	}

	public String getSentDateToString() {
		if (_sent_date != null) {
			return StringUtil.toDateTimeString(_sent_date);
		} else {
			return "";
		}
	}

	@Override
	public int[] getPrimaryKey() {
		int[] ids = new int[1];
		ids[0] = getId();
		return ids;
	}

	@Override
	public IModel readFromResultSet(ResultSet rs) throws SQLException {
		ChatModel model = null;

		model = new ChatModel();

		// read fields
		{
			int value = rs.getInt("id");
			if (!rs.wasNull()) {
				model.setId(value);
			}
		}
		{
			String value = rs.getString("sender");
			if (!rs.wasNull()) {
				model.setSender(value);
			}
		}
		{
			String value = rs.getString("receiver");
			if (!rs.wasNull()) {
				model.setReceiver(value);
			}
		}
		{
			String value = rs.getString("message");
			if (!rs.wasNull()) {
				model.setMessage(value);
			}
		}
		{
			Date value = rs.getTimestamp("sent_date");
			if (!rs.wasNull()) {
				model.setSentDate(value);
			}
		}

		return model;
	}

	@Override
	public PreparedStatement genSelectStatement(Connection dbConn, IModel query) throws SQLException {
		String sql = "SELECT * FROM sp_chat_select(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChatModel obj = (ChatModel) query;

		// set parameters
		st.setObject("id", obj.getId());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genSelectListStatement(Connection dbConn, IModelExt query) throws SQLException {
		String sql = "SELECT * FROM sp_chat_selectList(:limit, :offset, :id,:sender,:receiver,:message,:sent_dateFrom,:sent_dateTo)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChatModelExt obj = (ChatModelExt) query;

		if (obj.getLimit() != Integer.MIN_VALUE) {
			st.setObject("limit", obj.getLimit());
		} else {
			st.setObject("limit", Integer.MAX_VALUE);
		}
		if (obj.getOffset() != Integer.MIN_VALUE) {
			st.setObject("offset", obj.getOffset());
		} else {
			st.setObject("offset", 0);
		}

		// set parameters
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getSender() != null) {
			st.setObject("sender", obj.getSender());
		} else {
			st.setObject("sender", null);
		}
		if (obj.getReceiver() != null) {
			st.setObject("receiver", obj.getReceiver());
		} else {
			st.setObject("receiver", null);
		}
		if (obj.getMessage() != null) {
			st.setObject("message", obj.getMessage());
		} else {
			st.setObject("message", null);
		}
		if (obj.getSentDateFrom() != null) {
			st.setObject("sent_dateFrom", obj.getSentDateFrom());
		} else {
			st.setObject("sent_dateFrom", null);
		}
		if (obj.getSentDateTo() != null) {
			st.setObject("sent_dateTo", obj.getSentDateTo());
		} else {
			st.setObject("sent_dateTo", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genInsertStatement(Connection dbConn, IModel model) throws SQLException {
		String sql = "SELECT sp_chat_insert(:sender,:receiver,:message,:sent_date)";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChatModel obj = (ChatModel) model;

		// set parameters here
		if (obj.getSender() != null) {
			st.setObject("sender", obj.getSender());
		} else {
			st.setObject("sender", null);
		}
		if (obj.getReceiver() != null) {
			st.setObject("receiver", obj.getReceiver());
		} else {
			st.setObject("receiver", null);
		}
		if (obj.getMessage() != null) {
			st.setObject("message", obj.getMessage());
		} else {
			st.setObject("message", null);
		}
		if (obj.getSentDate() != null) {
			st.setObject("sent_date", obj.getSentDate());
		} else {
			st.setObject("sent_date", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genUpdateStatement(Connection dbConn, IModel model) throws SQLException {
		String sql = "SELECT sp_chat_update(:id,:sender,:receiver,:message,:sent_date)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChatModel obj = (ChatModel) model;

		// set parameters here
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getSender() != null) {
			st.setObject("sender", obj.getSender());
		} else {
			st.setObject("sender", null);
		}
		if (obj.getReceiver() != null) {
			st.setObject("receiver", obj.getReceiver());
		} else {
			st.setObject("receiver", null);
		}
		if (obj.getMessage() != null) {
			st.setObject("message", obj.getMessage());
		} else {
			st.setObject("message", null);
		}
		if (obj.getSentDate() != null) {
			st.setObject("sent_date", obj.getSentDate());
		} else {
			st.setObject("sent_date", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genDeleteStatement(Connection dbConn, IModel query) throws SQLException {
		String sql = "select sp_chat_delete(:id);";
		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChatModel obj = (ChatModel) query;

		// set primary keys
		st.setObject("id", obj.getId());

		return st.getPreparedStatement();
	}

	@Override
	public PreparedStatement genCountStatement(Connection dbConn, IModelExt input) throws SQLException {
		String sql = "SELECT sp_chat_count(:id,:sender,:receiver,:message,:sent_dateFrom,:sent_dateTo)";

		NamedParameterPreparedStatement st = new NamedParameterPreparedStatement(dbConn, sql);

		ChatModelExt obj = (ChatModelExt) input;

		// set parameters here
		if (obj.getId() != Integer.MIN_VALUE) {
			st.setObject("id", obj.getId());
		} else {
			st.setObject("id", null);
		}
		if (obj.getSender() != null) {
			st.setObject("sender", obj.getSender());
		} else {
			st.setObject("sender", null);
		}
		if (obj.getReceiver() != null) {
			st.setObject("receiver", obj.getReceiver());
		} else {
			st.setObject("receiver", null);
		}
		if (obj.getMessage() != null) {
			st.setObject("message", obj.getMessage());
		} else {
			st.setObject("message", null);
		}
		if (obj.getSentDateFrom() != null) {
			st.setObject("sent_dateFrom", obj.getSentDateFrom());
		} else {
			st.setObject("sent_dateFrom", null);
		}
		if (obj.getSentDateTo() != null) {
			st.setObject("sent_dateTo", obj.getSentDateTo());
		} else {
			st.setObject("sent_dateTo", null);
		}

		return st.getPreparedStatement();
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setCreator(int creator) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setUpdator(int updator) {
		// TODO Auto-generated method stub

	}

	@Override
	public Date getUpdated() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":id", Integer.toString(_id)) .replace(":sender",
	 * HtmlUtil.genHtml(_sender)) .replace(":receiver",
	 * HtmlUtil.genHtml(_receiver)) .replace(":message",
	 * HtmlUtil.genHtml(_message)) .replace(":sent_date",
	 * StringUtil.toDateTimeString(_sent_date))
	 * 
	 * ; return html; }
	 */

	/*
	 * private String fillHtmlTemplate(String template) { String html = template
	 * .replace(":Id", Integer.toString(_Id)) .replace(":Sender",
	 * HtmlUtil.genHtml(_Sender)) .replace(":Receiver",
	 * HtmlUtil.genHtml(_Receiver)) .replace(":Message",
	 * HtmlUtil.genHtml(_Message)) .replace(":SentDate",
	 * StringUtil.toDateTimeString(_SentDate))
	 * 
	 * ; return html; }
	 */

}